<?php
namespace Admin\Controller;
use Think\PageAdmin;
use Common\Support\Curl;
use Think\Upload;
use Common\Service\Sms;
/**
 * 用户管理控制器
 */
class UserController extends BaseController
{

    /**
     * 初始化
     * 
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();       
    }

    public function index(){
        $prefix = C('DB_PREFIX');

        $count = M('user')->count();
        $page  = new PageAdmin($count, C('PAGE_NUM'));
        
        $pages['show']  = $page->show();  //分页输出
        $pages['total'] = $count;  //总数
        $pages['lists'] = M('user a')
            ->order("a.id desc")
            ->limit($page->firstRow.','.$page->listRows)
            ->select();

        $this->seotitle = '用户列表';
        $this->pages    = $pages;
        $this->display();
    }

    public function add(){
        $this->seotitle = "添加用户";
        $this->display();
    }

    /**
     * 保存
     * @return [type] [description]
     */
    public function save()
    {
        $action = I('get.action', '', 'trim');
        $params = I('post.');

        if($params['mobile'] == ''){
            $this->error('请输入用户名/手机号');
        }

        if($params['nickname'] == ''){
            $this->error('请输入昵称');
        }

        $file = M('files')->where(['file_id'=>$params['headimg']])->find();
        if($file){
            $file_path = C('DOMAIN').$file['filepath'];
        }else{
            $file_path = $params['headimg'];
        }

        // 新增
        if ($action == 'add') {
            M('user')->add([
                'mobile'        => trim($params['mobile']),
                'nickname'      => $params['nickname'],                
                'headimg'       => $file_path,
                'status'        => $params['status'] == 1 ? 1 : 0,
                'now_status'    => 1,
                'code'          =>rand(11111111,99999999),
                'remark'        => $params['remark'],
                'description'   => $params['description'],
                'addtime'       => time(),
                'updatetime'    => time()
            ]);

            $this->success('操作成功', U('index'));
        }

        // 编辑保存
        elseif ($action == 'edit') {
            $id = I('post.id', 0, 'intval');        

            M('user')->where(['id' => $id])->save([
                'mobile'        => trim($params['mobile']),
                'nickname'      => $params['nickname'],                
                'headimg'       => $file_path,
                'status'        => $params['status'] == 1 ? 1 : 0,
                'now_status'    => 1,
                'remark'        => $params['remark'],
                'description'   => $params['description'],
                'updatetime'    => time()
            ]);

            $this->success('操作成功', U('index'));
        }
    }

    public function edit(){
        $id = I('get.id', 0, 'intval');

        if (empty($id) ||
            ! $info = M('user')->where(['id' => $id])->find())
        {
            $this->error('参数错误');
        }

        $this->seotitle  = '用户编辑';
        $this->info      = $info;
        $this->display();
    }

    public function delete(){
        $id = I('get.id', 0, 'intval');

        M('user')->where(['id' => $id])->delete();

        $this->success('操作成功');
    }

    public function send(){
        set_time_limit(0);
        header('Content-Type: text/plain; charset=utf-8');
        $response = Sms::sendCode();
        
        if($response->Message == 'OK'){
            $this->ajaxReturn(['status' => true, 'code'=>1000,'msg' => '短信发送成功','data'=>['phone'=>$phone,'num'=>$res['data']]]);
        }
    }

    

}